package com.egao.gps.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.biz.UserBiz;
import com.egao.common.system.entity.User;
import com.egao.gps.dao.UserLoginTimesDao;
import com.egao.gps.entity.UserLoginTimes;
import com.egao.gps.service.UserLoginTimesService;
import com.egao.orderB.biz.AddressFactoryBiz;
import com.egao.orderB.biz.BillBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Damon
 * @create 2020-11-17 14:31
 */
@Service
public class UserLoginTimesServiceImpl implements UserLoginTimesService {

    @Autowired
    private UserLoginTimesDao userLoginTimesDao;
    @Autowired
    private UserBiz userBiz;
    @Autowired
    private BillBiz billBiz;
    @Autowired
    private AddressFactoryBiz addressFactoryBiz;

    @Override
    public JsonResult listPage(PageResult2 page, UserLoginTimes userLoginTimes) {
        return userLoginTimesDao.listPage(page,userLoginTimes);
    }

    @Override
    public JsonResult list(UserLoginTimes userLoginTimes) {
//        if(Utils.isNotEmpty(userLoginTimes.getDate1())&&Utils.isNotEmpty(userLoginTimes.getDate2())){
//            try {
//                SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                Date begin = dfs.parse(userLoginTimes.getDate1());
//                Date end = dfs.parse(userLoginTimes.getDate2());
//                long between = (end.getTime() - begin.getTime()) / 1000;//除以1000是为了转换成秒
//                long day1 = between / (24 * 3600);
//                long hour1 = between % (24 * 3600) / 3600;
//                long minute1 = between % 3600 / 60;
//                long second1 = between % 60 / 60;
////                System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分" + second1 + "秒");
//                if(day1<1) {
//                    return JsonResult.ok().setData(userLoginTimesDao.list(userLoginTimes));
//                }
//            } catch (Exception e) {
//                return JsonResult.error("时间不能大于一天");
//            }
//        }
        return JsonResult.error();
    }



    @Override
    public JsonResult findById(UserLoginTimes userLoginTimes) {
        return JsonResult.ok().setData(userLoginTimesDao.findById(userLoginTimes));
    }


    @Override
    public boolean judgeLoginTimes(UserLoginTimes userLoginTimes) {
        UserLoginTimes userLoginTimes1 = userLoginTimesDao.findById(userLoginTimes);
        if(userLoginTimes1!=null) {
            int index = 1;
            if(DateUtil.pastDays(DateUtil.parseDate(userLoginTimes1.getCreateDate())) < 1){
                index = userLoginTimes1.getLoginTimes() + 1;
            } else {
                index = 1;
            }
            userLoginTimes1.setLoginTimes(index);
            userLoginTimes1.setCreateDate(DateUtil.getCurrentDate());
            userLoginTimesDao.update(userLoginTimes1);

            if(index >2) {
                User user = new User();
                user.setState("1");
                user.setUsername(userLoginTimes.getUsername());
                userBiz.updateUserState(user, new User(1));
                return false;
            }
            return true;
        } else {
            userLoginTimes.setLoginTimes(1);
            userLoginTimes.setCreateDate(DateUtil.getCurrentDate());
            userLoginTimesDao.save(userLoginTimes);
            return true;
        }
    }

    @Override
    public void thaw(UserLoginTimes userLoginTimes) {
        UserLoginTimes userLoginTimes1 = userLoginTimesDao.findById(userLoginTimes);
        if(userLoginTimes1!=null) {
            userLoginTimes1.setLoginTimes(0);
            userLoginTimes1.setCreateDate(DateUtil.getCurrentDate());
            userLoginTimesDao.update(userLoginTimes1);
        }
    }

    @Override
    public JsonResult save(UserLoginTimes userLoginTimes) {
        userLoginTimesDao.save(userLoginTimes);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult saveBatch(List<UserLoginTimes> userLoginTimesList) {
        userLoginTimesDao.saveBatch(userLoginTimesList);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult update(UserLoginTimes userLoginTimes) {
        userLoginTimesDao.update(userLoginTimes);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult remove(UserLoginTimes userLoginTimes) {
        userLoginTimesDao.remove(userLoginTimes);
        return JsonResult.ok("删除成功");
    }

}
